package com.dub.utils;

import java.nio.ByteBuffer;

public class ByteConvert{
    public static byte[] shortToBytes(short n){
        ByteBuffer buffer = ByteBuffer.allocate(2);
        buffer.putShort(n);
        return buffer.array();
    }
    public static short bytesToShort(byte[] b){
        ByteBuffer buffer = ByteBuffer.allocate(2);
        buffer.put(b,0,b.length);
        buffer.flip();
        return buffer.getShort();
    }
    public static byte[] intToBytes(int num){
        ByteBuffer buffer = ByteBuffer.allocate(4);
       buffer.putInt(num);
        return buffer.array();
    }
    public static int bytesToInt(byte[] b){
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.put(b,0,b.length);
        buffer.flip();
        return buffer.getInt();
    }
    public static byte[] longToBytes(long num){
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(0,num);
        return buffer.array();
    }
    public static long bytesToLong(byte[] b){
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.put(b, 0, b.length);
        buffer.flip();
        return buffer.getLong();
    }
}